import 'package:flutter/material.dart';
import 'package:honeypotflutter/public/public_date.dart';
import 'package:honeypotflutter/publicBussine/bussine_colors.dart';
import 'package:honeypotflutter/publicBussine/bussine_font.dart';
import 'package:honeypotflutter/publicBussine/bussine_image.dart';
import 'package:honeypotflutter/publicBussine/view/bussine_card.dart';
import 'package:honeypotflutter/publicBussine/view/bussine_item.dart';
import '../help/package_help.dart';
import '../model/package_list_model.dart';

class PackageListItem extends StatefulWidget {
  final PackageListItemModel itemModel;
  final PackageType type;
  final String? firstButtonTitle;
  final String? secondButtonTitle;
  final Function()? firstAction;
  final Function()? secondAction;

  /// 修改手机号
  final Function()? modifyPhoneAction;
  const PackageListItem({
    super.key,
    this.type = PackageType.waitPackage,
    this.firstButtonTitle,
    this.secondButtonTitle,
    this.firstAction,
    this.secondAction,
    this.modifyPhoneAction,
    required this.itemModel,
  });

  @override
  State<PackageListItem> createState() => _PackageListItemState();
}

class _PackageListItemState extends State<PackageListItem> {
  @override
  Widget build(BuildContext context) {
    return MGCard(
      margin: EdgeInsets.only(left: 12, right: 12, top: 12),
      children: [
        _headerWidget(),
        SizedBox(height: 5),
        _detailWidget(title: "快递公司", content: widget.itemModel.expressName),
        (widget.type == PackageType.acquiredPackage) ? _detailWidget(title: "手机号码", content: widget.itemModel.orderPhone) : _detailIphone(),
        _detailWidget(title: "柜机编号", content: widget.itemModel.tmcDoorsTmcLockBoardsTmcDevicesDisPlayNo),
        _detailWidget(title: "柜机名称", content: widget.itemModel.deviceName),
        _detailWidget(title: "柜机格口", content: widget.itemModel.tmcDoorsDisplayAlias),
        _detailWidget(title: "入柜时间", content: HLDate.hl_formatDate(widget.itemModel.depositTime)),
        _detailWidget(title: "超市时间", content: HLDate.hl_formatDate(widget.itemModel.overdueTime)),
        (widget.type == PackageType.acquiredPackage) ? _detailWidget(title: "取走时间", content: HLDate.hl_formatDate(widget.itemModel.takeTime)) : Container(),
        (widget.type == PackageType.acquiredPackage) ? _detailWidget(title: "取走方式", content: widget.itemModel.takePackageModeDisplayName, contentColor: MGColors.blueColor) : Container(),
        _bottomWidget(),
      ],
    );
  }

  /*
  * 头部
  * */
  Widget _headerWidget() {
    return MGItems(
      padding: EdgeInsets.only(top: 12, bottom: 12),
      leadTitle: "运单号:${widget.itemModel.expressNo}",
      leadStyle: TextStyle(
        color: MGColors.text1Color,
        fontSize: MGFont.textFont1,
        fontWeight: FontWeight.bold,
      ),
      trailingTitle: "${widget.itemModel.doorStateStr}",
      trailingStyle: TextStyle(
        color: widget.type == PackageType.retentionPackage ? MGColors.redColor : MGColors.themeColor,
        fontSize: MGFont.textFont1,
      ),
      showBottomSplit: true,
    );
  }

  /*
  * 内容
  * */
  Widget _detailWidget({required String title, String? content, Color? contentColor}) {
    return MGItems(
      padding: EdgeInsets.only(top: 5, bottom: 5),
      leadTitle: "${title}:",
      leadStyle: TextStyle(
        fontSize: MGFont.textFont2,
        color: MGColors.text3Color,
      ),
      trailingTitle: content ?? "",
      trailingStyle: TextStyle(
        fontSize: MGFont.textFont2,
        color: contentColor ?? MGColors.text1Color,
      ),
      mainAxisAlignment: MainAxisAlignment.start,
    );
  }

  /*
  * 内容
  * */
  Widget _detailIphone() {
    return MGItems(
      padding: EdgeInsets.only(top: 5, bottom: 5),
      leadTitle: "手机号码:",
      leadStyle: TextStyle(
        fontSize: MGFont.textFont2,
        color: MGColors.text3Color,
      ),
      trailingWidget: Row(
        children: [
          Text(
            widget.itemModel.orderPhone ?? "",
            style: TextStyle(
              fontSize: MGFont.textFont2,
              color: MGColors.text1Color,
            ),
          ),
          SizedBox(width: 10),
          InkWell(
              onTap: widget.modifyPhoneAction,
              child: Image.asset(
                MGImage.image("public_edit"),
                width: 15,
              ))
        ],
      ),
      mainAxisAlignment: MainAxisAlignment.start,
    );
  }

  /*
  * 底部
  * */
  Widget _bottomWidget() {
    return Container(
        padding: EdgeInsets.only(top: 15, bottom: 15),
        child: Row(
          children: [
            Row(
              children: [
                Image.asset(MGImage.image("public_monry"), width: 23),
                SizedBox(width: 5),
                Text(
                  "费用: ",
                  style: TextStyle(
                    color: MGColors.text1Color,
                    fontSize: MGFont.textFont1,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                Text(
                  (widget.itemModel.cabinetFee ?? 0.0).toStringAsFixed(2),
                  style: TextStyle(
                    color: MGColors.text1Color,
                    fontSize: MGFont.textFont1,
                    fontWeight: FontWeight.bold,
                  ),
                )
              ],
              mainAxisAlignment: MainAxisAlignment.start,
            ),
            Row(
              children: [
                (widget.firstButtonTitle != null)
                    ? InkWell(
                        child: Container(
                          padding: EdgeInsets.only(left: 20, right: 20, top: 8, bottom: 8),
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(20),
                            border: Border.all(color: MGColors.splitColor, width: 1),
                          ),
                          child: Text(
                            widget.firstButtonTitle!,
                            style: TextStyle(
                              color: MGColors.text1Color,
                              fontSize: MGFont.textFont2,
                            ),
                          ),
                        ),
                        onTap: widget.firstAction,
                      )
                    : Container(),
                (widget.secondButtonTitle != null)
                    ? InkWell(
                        child: Container(
                          margin: EdgeInsets.only(left: 10),
                          padding: EdgeInsets.only(left: 20, right: 20, top: 8, bottom: 8),
                          decoration: BoxDecoration(
                            color: MGColors.themeColor,
                            borderRadius: BorderRadius.circular(20),
                          ),
                          child: Text(
                            widget.secondButtonTitle!,
                            style: TextStyle(
                              color: MGColors.text1Color,
                              fontSize: MGFont.textFont2,
                            ),
                          ),
                        ),
                        onTap: widget.secondAction,
                      )
                    : Container(),
              ],
              mainAxisAlignment: MainAxisAlignment.end,
            )
          ],
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
        ));
  }
}
